//
//  YZMShareService.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 2016/12/14.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMShareService.h"
#import "YZMAlertView.h"

@implementation YZMShareService

+ (instancetype)sharedService
{
    static YZMShareService *sInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sInstance = [[self alloc] init];
    });
    
    return sInstance;
}


- (instancetype)init{
    
    if (self = [super init]) {
        
        //按数组内容顺序显示图标
        [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_QQ), @(UMSocialPlatformType_Sina), @(UMSocialPlatformType_WechatSession), @(UMSocialPlatformType_WechatTimeLine)]];
        
        //调整分享面板
        [UMSocialShareUIConfig shareInstance].shareTitleViewConfig.shareTitleViewTitleString = @"分享到";
        [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerMarginBottom = 10;
        [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.isShow = NO;
    }
    
    return self;
}

- (void)showSharePageTable:(NSDictionary *)dic {
    
    //显示分享面板
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        
        switch (platformType) {
            case UMSocialPlatformType_WechatSession:
                
                [self shareTextToPlatformType:UMSocialPlatformType_WechatSession withContent:dic];
                break;
            case UMSocialPlatformType_WechatTimeLine:
                
                [self shareTextToPlatformType:UMSocialPlatformType_WechatTimeLine withContent:dic];
                break;
            case UMSocialPlatformType_QQ:
                
                [self shareTextToPlatformType:UMSocialPlatformType_QQ withContent:dic];
                break;
            case UMSocialPlatformType_Sina:
                
                [self shareTextToPlatformType:UMSocialPlatformType_Sina withContent:dic];
                break;
            default:
                break;
        }
        
    }];
}

- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType withContent:(NSDictionary *)dic
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    if (dic[@"url"]) {
        
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:dic[@"title"] descr:dic[@"describe"] thumImage:[UIImage imageNamed:@"icon_app"]];
        //设置网页地址
        shareObject.webpageUrl = dic[@"url"];
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
    } else if (dic[@"image"]){
        UMShareImageObject *shareObject=[[UMShareImageObject alloc]init];
        shareObject.thumbImage=dic[@"image"];
        shareObject.shareImage = dic[@"image"];
        
        messageObject.shareObject=shareObject;
        
    } else {
        //创建文字内容对象
        messageObject.text = dic[@"describe"];
    }
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        
        YZMAlertView *alertView = [[YZMAlertView alloc] init];
        alertView.contentLabel.textAlignment = NSTextAlignmentCenter;
        alertView.cancelButton.hidden = YES;
        if (error) {
            
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            
            alertView.contentLabel.text = @"分享成功";
            [alertView showInView:YZMHCMInstance.currentNavationCtroller.view];
            NSLog(@"response data is %@",data);
        }
    }];
}

@end
